// 2023-03-20 16:42:32

class TreeNode {
    /**
     * @param {string} value 节点值
     * @param {TreeNode} left 
     * @param {TreeNode} right 
     */
    constructor(value, left, right) {
        this.value = (value === undefined ? 0 : value);
        this.left = (left === undefined ? null : left);
        this.right = (right === undefined ? null : right);
    }
    /**
     * 广序生成二叉树
     * @param {Array} array 
     */
    static CreateTree(array) {
        if (array.length < 1) return null;
        let root = new TreeNode(array[0]);
        //使用队列保证广序节点顺序
        const queue = [];
        queue.push(root);
        let current = root;
        let node;
        for (let i = 1; i < array.length; i++) {
            if (array[i]) {
                node = new TreeNode(array[i]);
            } else node = null
            if (i % 2 == 1) {
                current = queue.shift();
                current.left = node;
            } else {
                current.right = node;
            }
            queue.push(node);
        }
        return root;
    }
}

class Tree {
    constructor() {

    }
}

/**
 * 定义WebSocket发送信息的数据类型
 */
class WebSocketData {
    /**
     * 定义数据WebSocket传输的数类型
     * @param {string} msg A valid JSON string {msg:信息载体}
     * @param { {_initInfo:false,sendID:string,receiverID:string} | { _initInfo: true,userID:String }} options
     */
    constructor(msg = '', options = {}) {
        this.headers = {}
        this.data = {
            msg
        }
        try {
            if (options._initInfo) {
                if (options.userID) {
                    this.headers['userID'] = options.userID
                    this.headers['_initInfo'] = true
                } else throw TypeError('userID不能为空')
            } else {
                if (options.receiverID && options.sendID) {
                    this.headers['_initInfo'] = false
                    this.headers['sendID'] = options.sendID
                    this.headers['receiverID'] = options.receiverID
                } else throw TypeError('sendID或receiverID不能为空')
            }
            // 携带token
            let info = window.localStorage.getItem('HuiLY')
            let token = JSON.parse(info).token ? JSON.parse(info).token : 'token'
            this.headers['authorization'] = `huily ${token}`
        } catch (error) {
            console.error(error);
        }
    }
}



export {
    WebSocketData
}